package com.lanou3g.musicenjoy.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.MyFavoriteAdapter;
import com.lanou3g.musicenjoy.custom.SlideDeleteListView;
import com.lanou3g.musicenjoy.entity.Music;
import com.lanou3g.musicenjoy.entity.PlayMusicEntity;
import com.lanou3g.musicenjoy.entity.greendao.MusicInfoBean;
import com.lanou3g.musicenjoy.entity.greendao.MusicInfoBeanDao;
import com.lanou3g.musicenjoy.eventbus.MyEvent;
import com.lanou3g.musicenjoy.greendao.DaoSingleton;
import com.lanou3g.musicenjoy.service.OnlineService;
import com.umeng.socialize.utils.Log;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;
import de.greenrobot.event.EventBus;


/**
 * Created by 李宁 on 15/11/8.
 * 我的最爱页面
 */
public class MyFavoriteFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener, SlideDeleteListView.RemoveListener,SlideDeleteListView.OnItemLongClickListener {
    private ImageView back;//返回
    private SlideDeleteListView myfavorite_deletelist;//可删除ListView
    private MyFavoriteAdapter adapter;//我的最爱适配器
    private DaoSingleton daoSingleton;
    private MusicInfoBean musicInfoBean;
    private MusicInfoBeanDao musicInfoBeanDao;
    private QueryBuilder<MusicInfoBean> queryBuilder;
    private ArrayList<MusicInfoBean> infos;
    private List<PlayMusicEntity> datas;
    private OnlineService.MediaBinder mediaBinder;//查询数据库表用



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_myfavorite,null);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        back = (ImageView) getActivity().findViewById(R.id.back);
        myfavorite_deletelist = (SlideDeleteListView) getActivity().findViewById(R.id.myfavorite_deletelist);

        //dao初始化
        daoSingleton = DaoSingleton.getInstance();
        musicInfoBeanDao = daoSingleton.getMusicInfoBeanDao();
        queryBuilder = musicInfoBeanDao.queryBuilder();
        infos = (ArrayList<MusicInfoBean>) musicInfoBeanDao.loadAll();

        adapter = new MyFavoriteAdapter(getActivity(),infos);
        myfavorite_deletelist.setAdapter(adapter);
        //轻量级缓存 传歌曲数量
        SharedPreferences preferencesnum = getActivity().getSharedPreferences("lining", Context.MODE_PRIVATE); //我的最爱
        SharedPreferences.Editor editornum = preferencesnum.edit();//我的最爱首
        editornum.putInt("countnum", infos.size());//我的最爱首
        editornum.commit();//我的最爱
        editornum.clear();//我的最爱
        //监听
        back.setOnClickListener(this);//返回
        myfavorite_deletelist.setOnItemClickListener(this);//播放
        myfavorite_deletelist.setRemoveListener(this);//移动删除
        myfavorite_deletelist.setOnItemLongClickListener(this);//长按删除
    }



    @Override
    public void onClick(View v) {
        //返回上一个fragment
        getFragmentManager().popBackStack();
    }

    //播放
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        datas=new ArrayList<>();
        PlayMusicEntity entity=new PlayMusicEntity();
        entity.setUrl(infos.get(position).getUrl());
        entity.setPicUrl(infos.get(position).getPicUrl());
        entity.setSingerName(infos.get(position).getSinger());
        entity.setSongName(infos.get(position).getSongname());
        datas.add(entity);
        EventBus.getDefault().post(new MyEvent.PassData(datas,position));

    }

    //移动删除
    @Override
    public void removeItem(SlideDeleteListView.RemoveDirection direction, final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("删除收藏");
        builder.setMessage("确认删除收藏?");
        builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                adapter.remove(adapter.getItem(position));
                Toast.makeText(getActivity(), "已取消收藏", Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();

    }

    //长按删除
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        musicInfoBean = (MusicInfoBean) parent.getItemAtPosition(position);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("删除收藏");
        builder.setMessage("确认删除收藏?");
        builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String songName = musicInfoBean.getSongname();
                String singer = musicInfoBean.getSinger();
                Log.i("xxx", songName + 111);
                DeleteQuery<MusicInfoBean> deleteQuery = queryBuilder.where(MusicInfoBeanDao.Properties.Songname.eq(songName)).buildDelete();
                deleteQuery.executeDeleteWithoutDetachingEntities();
                adapter.longClickRefresh(musicInfoBean);
                Toast.makeText(getActivity(), "已取消收藏", Toast.LENGTH_LONG).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
        return false;
    }



}
